/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.core.players;

import com.chasekernan.hxnova.core.dataholders.Global;
import com.chasekernan.hxnova.core.dataholders.MessageHandler;
import com.chasekernan.hxnova.core.dataholders.Players;
import com.chasekernan.hxnova.core.dataholders.SalvageHolder;
import com.chasekernan.hxnova.core.dataholders.StarsHolder;
import com.chasekernan.hxnova.core.fleets.Fleet;
import com.chasekernan.hxnova.core.dataholders.FleetHolder;
import com.chasekernan.hxnova.utils.Utils;
import com.chasekernan.hxnova.core.players.Player;
import com.chasekernan.hxnova.server.Game;

/**
    All new player information for a turn is dumped into this file, which is
    later saved and distributed to the player.
**/
class PlayerTurnFile {
    
    public var year : Int;
    public var score : Int;
    public var rank : Int;
    public var stars : StarsHolder;
    public var fleets : FleetHolder;
    public var player : com.chasekernan.hxnova.core.players.Player;
    public var salvage : SalvageHolder;
    public var players : Players;
    public var game : Game;
    
    /**
        Creates a new turn file for player [p] and uses the year specified in
        [Global].
    **/
    public function new(p : Player) {
        player = p;
        year = Global.year;
        
        stars = new StarsHolder();
        fleets = new FleetHolder();
        salvage = new SalvageHolder();
        
        players = new Players();
        for (p in Global.players) {
            var copy = new Player();
            copy.id = p.id;
            players.players.push(copy);
        }
        
        game = new Game();
    }
    
    public static var XML_TITLE = "turn_file";
    
    /**
        Writes a player's turn to xml.
        Sample xml:
            TODO: run [writeToXml]
    **/
    public static function writeToXml(ptf : PlayerTurnFile) : Xml {
        var xml = Xml.parse('<' + XML_TITLE + '></' + XML_TITLE + '>')
                .firstChild();
                
        xml.set('year', Std.string(ptf.year));
        xml.set('score', Std.string(ptf.score));
        xml.set('rank', Std.string(ptf.rank));
        
        //TODO: Compiler bug, [MessageHandler#Target.Player is overriding class
        //[Player]
        xml.addChild(com.chasekernan.hxnova.core.players.Player.writeToXml(
            ptf.player));
        
        xml.addChild(StarsHolder.writeToXml(ptf.stars));
        
        xml.addChild(FleetHolder.writeToXml(ptf.fleets));
        
        xml.addChild(MessageHandler.writePlayerMessagesToXml(ptf.player));
        
        xml.addChild(SalvageHolder.writeToXml(ptf.salvage));
        
        xml.addChild(Players.writeToXml(ptf.players));
        
        xml.addChild(Game.writeToXml(ptf.game, true));
        
        return xml;
    }
    
    public static function readFromXml(xml : Xml) : PlayerTurnFile {
        try {
            var player = com.chasekernan.hxnova.core.players.Player.readFromXml(
                    Utils.getXmlElement(xml, 
                    com.chasekernan.hxnova.core.players.Player.XML_TITLE));
                
            var ptf = new PlayerTurnFile(player);
            ptf.score = Std.parseInt(xml.get('score'));
            ptf.year = Std.parseInt(xml.get('year'));
            ptf.rank = Std.parseInt(xml.get('rank'));
            
            ptf.stars = StarsHolder.readFromXml(Utils.getXmlElement(xml, 
                    StarsHolder.XML_TITLE));
            
            ptf.fleets = FleetHolder.readFromXml(Utils.getXmlElement(xml, 
                    FleetHolder.XML_TITLE));
                    
            MessageHandler.readPlayerMessagesFromXml(Utils.getXmlElement(xml, 
                    MessageHandler.XML_TITLE));
            
            ptf.salvage = SalvageHolder.readFromXml(Utils.getXmlElement(xml, 
                    SalvageHolder.XML_TITLE));
                    
            ptf.players = Players.readFromXml(Utils.getXmlElement(xml,
                    Players.XML_TITLE));
                    
            ptf.game = Game.readFromXml(Utils.getXmlElement(xml,
                    Game.XML_TITLE));
                    
            return ptf;
        } catch (e : Dynamic) {
            throw "Error while reading from player turn file: " + e;
        }
    }
    
}